/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utp.tepc.padroes.projeto.observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

class Turno extends TimerTask
{
    List<Reator> reatores = new ArrayList<Reator>();
    Monitor monitor;
    private int maxTurno = 60;
    private int actualTurno = 0;
    private Timer timer;

    public Turno(Timer t)
    {
        this.timer = t;
        
        this.monitor = new Monitor();
	
        this.reatores.add(new Reator(1, 30, 20, "Reator A"));
        this.reatores.add(new Reator(1.1f, 65, 40, "Reator B"));
        this.reatores.add(new Reator(1.5f, 170, 100, "Reator C"));
        this.reatores.add(new Reator(3, 236, 210, "Reator D"));
        
        for(Reator r : reatores)
            r.registrarObserver(monitor);
    }

    public void run()
    {
        this.actualTurno++;

        System.out.println("-----------------------------------------------------------");
        System.out.println("------------------- TURNO "+ actualTurno + " -----------------------------");
        System.out.println("-----------------------------------------------------------");
        for(Reator r : reatores)
        {
            r.equalizar();
            
            if(r.getIsRun())
        	r.iniciarOperacao();
        }

        System.out.println("-----------------------------------------------------------");
        System.out.println("------------------- FINAL DO TURNO ------------------------");
        System.out.println("-----------------------------------------------------------");
        if(this.actualTurno >= this.maxTurno){
            timer.cancel(); //Terminate the timer thread
            System.out.println("Final da simulação");
        }
    }
}
